package com.minhua.jijiehao2.mainframe.circle.businessListActivity.impl

import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.recyclerview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.BusinessBean
import com.minhua.jijiehao2.mainframe.circle.businessDetailActivity.impl.BusinessDetailActivity
import com.minhua.jijiehao2.mainframe.circle.businessListActivity.imp.IBusinessListView
import kotlinx.android.synthetic.main.activity_business_list.*

class BusinessListActivity : BaseActivity<IBusinessListView, BusinessListPresenter>(), IBusinessListView {

    override fun getLayoutId(): Int = R.layout.activity_business_list

    override fun getPresenter(): BusinessListPresenter = BusinessListPresenter()


    companion object {
        fun intentStart(context: Context, type: String) {
            val intent = Intent(context, BusinessListActivity::class.java)
            intent.putExtra("type", type)
            context.startActivity(intent)
        }
    }

    private var isFrist = true
    override fun onResume() {
        super.onResume()
        if (isFrist) {
            isFrist = false
        } else {
            initData()
        }
    }

    private val listBusiness = arrayListOf<BusinessBean>()
    private var adapter: BaseCommonAdapter<BusinessBean>? = null
    override fun initView() {
        refresh.setOnRefreshListener { initData() }

        adapter = object : BaseCommonAdapter<BusinessBean>(this, R.layout.item_layout_business, listBusiness) {
            override fun convert(holder: ViewHolder?, t: BusinessBean?) {
                Glide.with(this@BusinessListActivity).load(t?.logo_path).into(holder?.getView(R.id.ivIcon))
                holder?.setText(R.id.tvItemName, t?.proc_name)
                holder?.setText(R.id.tvItemMessage, t?.introduction)
                holder?.setText(R.id.tvCount, t?.show_click_num)
                holder?.setOnClickListener(R.id.itemView, { BusinessDetailActivity.intentStart(this@BusinessListActivity, t!!) })
            }
        }
        rvContent.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        rvContent.adapter = adapter

    }

    override fun initData() {
        mPresenter.getBusinessList(intent.getStringExtra("type"))
    }

    override fun onListSuccess(list: List<BusinessBean>) {
        refresh.isRefreshing = false
        listBusiness.clear()
        listBusiness.addAll(list)
        adapter?.notifyDataSetChanged()
        if (listBusiness.isNotEmpty()) {
            rvContent.visibility = View.VISIBLE
            ivEmpty.visibility = View.GONE
        } else {
            rvContent.visibility = View.GONE
            ivEmpty.visibility = View.VISIBLE
        }
    }
}
